package com.zxq.common.middleware.emuns;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@AllArgsConstructor
@Getter
public enum MasterSlaveEnum {
    MASTER(1, "主"),
    SLAVE(2, "备");

    private Integer code;
    private String desc;

    private static Map<Integer, MasterSlaveEnum> codeMap = new HashMap<>();

    static {
        codeMap.putAll(Arrays.stream(MasterSlaveEnum.values())
                .collect(Collectors.toMap(MasterSlaveEnum::getCode, Function.identity())));
    }

    public static MasterSlaveEnum getByCode(int code) {
        return codeMap.getOrDefault(code, MASTER);
    }

}
